package anc.tankshooter.ui;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener;
import org.andengine.engine.camera.hud.controls.DigitalOnScreenControl;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;

import anc.tankshooter.GameActivity;
import anc.tankshooter.GameSetting;
import anc.tankshooter.controls.BossHeartBar;
import anc.tankshooter.controls.FlyText;
import anc.tankshooter.controls.InfoBoard;
import anc.tankshooter.database.HighScore;
import anc.tankshooter.models.Bullet;
import anc.tankshooter.models.Bullet.BulletType;
import anc.tankshooter.models.Enermy;
import anc.tankshooter.models.Enermy.EnermyType;
import anc.tankshooter.models.Explode;
import anc.tankshooter.models.IDymanicObject.MovingDirection;
import anc.tankshooter.models.ITank;
import anc.tankshooter.models.Tank;
import anc.tankshooter.models.Utils;
import anc.tankshooter.ui.ConfirmScreen.ButtonDialogWatcher;
import anc.tankshooter.ui.ResourcesManager.typeMusic;
import anc.tankshooter.ui.ResultScreen.ResultDialogWatcher;
import anc.tankshooter.ui.SceneManager.SceneType;

public class GameScreen extends IScreen implements IOnScreenControlListener,ResultDialogWatcher {

	private static Random mRadom=new Random();
	/**
	 * Điều khiển xe tăng di chuyển
	 */
	private DigitalOnScreenControl mController;
	/**
	 * Nút bắn 
	 */
	private Sprite mShootController;
	
	private AnimatedSprite mHighScoreButton;
	private AnimatedSprite mPauseButton;
	private AnimatedSprite mSettingButton;

	
	
		
	/**
	 * Xe tăng của người chơi.
	 */
	private Tank mTank;
	private ArrayList<ITank> mItanks;
	private ArrayList<Bullet> mBullets;
	private ArrayList<Explode> mExplodes;
	private ArrayList<FlyText> mFlyTexts;
	private ArrayList<BossHeartBar> mBossHeartBars;
	
	private int mCreateNormal,mCreateFast,mCreateHeavy,mCreateBoss;
	/**
	 * Lưu toàn bộ số điểm đã đạt được trong màn chơi trước.
	 */
	private HighScore mSavedScore;
	
	private boolean isSetting = false;
	private InfoBoard mInfoBoard;
	private ResultScreen mResultDialogScreen;
	private HighScoreScreen mHighScoreDialogScreen;

	@Override
	public void createScene() 
	{
		mSavedScore = new HighScore();
		mItanks = new ArrayList<ITank>();
		mExplodes = new ArrayList<Explode>();
		mBullets = new ArrayList<Bullet>();
		mFlyTexts = new ArrayList<FlyText>();
		mBossHeartBars = new ArrayList<BossHeartBar>();
		
		
		mTank=new Tank(0, 0);
		
		mController= new DigitalOnScreenControl(32, GameActivity.CAMERA_HEIGHT - 128 - 32, 
				mEngine.getCamera(),
				mResourcesManager.mControllerTexture,
				mResourcesManager.mKnobTexture, 
				0.04f,
				mVertextManager, this);
		
		mController.getControlBase().setAlpha(0.1f);
		mController.getControlBase().setScale(1.5f);
		mController.getControlKnob().setScale(1.5f);
		mController.refreshControlKnobPosition();

		int shootControllerX = GameActivity.CAMERA_WITDH - 96;
		int shootControllerY = GameActivity.CAMERA_HEIGHT - 96 ;
		mShootController = new Sprite(shootControllerX, shootControllerY, mResourcesManager.mShootTexture, mVertextManager) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
					if(!mTank.isDead()) {
						mTank.mIsAutoShoot=true;
						mShootController.setAlpha(0.5f);
					}
					return true;
				}else if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP||
						pSceneTouchEvent.getAction() == TouchEvent.ACTION_OUTSIDE)
					mShootController.setAlpha(0.1f);
				return false;
			}
		};
		mShootController.setScale(2f);
		mShootController.setAlpha(0.1f);
		mShootController.setZIndex(3);
		
		mInfoBoard=new InfoBoard(0,0); 
		mInfoBoard.setZIndex(3);
		mSettingButton = new AnimatedSprite(740, 5, ResourcesManager.getInstance().mSettingButtonTexture, mVertextManager)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN)
				{
					this.setCurrentTileIndex(1);
				}
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP)
				{
					mPauseButton.setCurrentTileIndex(1);
					if(!isSetting)
					{
						this.setCurrentTileIndex(0);
						SettingScreen settingScene = new SettingScreen(new SettingScreen.SettingButtonListener() {
							
							@Override
							public void onOkClicked() {
								exitSettingScene();
							}
							
							@Override
							public void onCancleClicked() {
								exitSettingScene();
							}
						});
						
						GameScreen.this.setChildScene(settingScene);
						pauseGame();
						isSetting = true;
					}else
					{
						GameScreen.this.clearChildScene();
						GameScreen.this.setChildScene(mController);
						resumeGame();
						isSetting = false;
					}
				}
				// TODO Auto-generated method stub
				return super
						.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		mPauseButton = new AnimatedSprite(680, 5, ResourcesManager.getInstance().mPauseButtonTexture, mVertextManager)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN)
				{
					if(!mIsPause)
					{
						this.setCurrentTileIndex(2);
						this.setAlpha(1);
					}
					else
					{
						this.setCurrentTileIndex(3);
						this.setAlpha(1);
					}
					
				}
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP)
				{
					if(!mIsPause)
					{
						this.setCurrentTileIndex(1);
						pauseGame();
					}
					else
					{
						this.setAlpha(0.5f);
						this.setCurrentTileIndex(0);
						resumeGame();
					}
					
				}
				
				// TODO Auto-generated method stub
				return super .onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mHighScoreButton = new AnimatedSprite(620, 5, ResourcesManager.getInstance().mHighScoreButtonTexture, mVertextManager)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN)
				{
					if(!mIsPause){
						this.setCurrentTileIndex(1);
					}
				}
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP)
				{
					if(!mIsPause){
						this.setAlpha(0.5f);
						this.setCurrentTileIndex(0);
						onHighScoreClick();
					}
				}
				
				// TODO Auto-generated method stub
				return super .onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		mHighScoreDialogScreen = new HighScoreScreen(true, new ButtonDialogWatcher() {
			
			@Override
			public void onOkClick() {
				clearChildScene();
				setChildScene(mController);
				mIsPause=false;
			}
			
			@Override
			public void onCancleClick() {
			}
		});
		
		onPlayAgain();
		
		ResourcesManager.getInstance().playMusic(typeMusic.GAME);
		ResourcesManager.getInstance().playMusic(typeMusic.FIGHTING);
	}



	private void onHighScoreClick() {
		mIsPause = true;
		ArrayList<HighScore> hss = GameActivity.mDatabase.getHighScores(10);
		mHighScoreDialogScreen.setHighScoreData(hss);
		setChildScene(mHighScoreDialogScreen);
	}



	@Override
	public boolean onBackKeyPressed() {
		if(!mIsPause){
			mIsPause = true;
			ConfirmScreen sc =new ConfirmScreen("Do you want to exit game?",new ButtonDialogWatcher() {
				
				@Override
				public void onOkClick() {
					ResourcesManager.getInstance().loadLevelSelectionResources();
					PickLevelScreen sc=new PickLevelScreen();
					SceneManager.getInstance().setScene(sc);
					mIsPause=false;
				}
				
				@Override
				public void onCancleClick() {
					clearChildScene();
					setChildScene(mController);
					mIsPause=false;
				}
			});
			
			setChildScene(sc);
			
		}
		return true;
	}

	@Override
	public void onControlChange(BaseOnScreenControl pBaseOnScreenControl, float pValueX, float pValueY) {
		if(!mIsPause)
		{
			MovingDirection oldDir = mTank.getDir();
			if (pValueY < 0 && !mTank.isMoving) 
				mTank.setDir(MovingDirection.UP);
			else if(pValueX > 0 && !mTank.isMoving)
				mTank.setDir(MovingDirection.RIGHT);
			else if(pValueY > 0 && !mTank.isMoving)
				mTank.setDir(MovingDirection.DOWN);
			else if(pValueX < 0 && !mTank.isMoving)
				mTank.setDir(MovingDirection.LEFT);
			
			if(pValueX!=0 || pValueY!=0) {
				if(oldDir == mTank.getDir() && !mTank.isTouchEdge() && !mTank.isMoving) 
					mTank.doMove();
			}
		}	
	}
	
	private void pauseGame()
	{
		mIsPause = true;
		ResourcesManager.getInstance().pauseMusic();
	}
	
	private void resumeGame()
	{
		mIsPause = false;
		ResourcesManager.getInstance().playMusic(ResourcesManager.typeMusic.GAME);
		ResourcesManager.getInstance().playMusic(ResourcesManager.typeMusic.FIGHTING);
	}
	

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_GAME;
	}

	@Override
	public void disposeScene() {
		
	}
	
	public void exitSettingScene()
	{
		this.clearChildScene();
		isSetting = false;
		this.setChildScene(mController);
		resumeGame();
	}


	@Override
	public void onUpdateScreen(float second) {
		if(!mIsPause) {
			GameActivity.mGlobalMap.updateMarkedMatrix(mItanks);
			for (int i = 0; i < mFlyTexts.size(); i++) {
				mFlyTexts.get(i).onUpdate();
				if(mFlyTexts.get(i).isIsDead())
				{
					mFlyTexts.get(i).detachSelf();
					mFlyTexts.remove(i); i--;
				}
			}
			for (int i = 0; i < mBullets.size(); i++) {
				Bullet bullet = mBullets.get(i);

				for(int j=0;j<mBullets.size();j++){
					Bullet b = mBullets.get(j);
					if(!b.isDead()&& bullet!= b && b.getShooter()!=bullet.getShooter() && bullet.collidesWith(b)){
						bullet.setDead(true);
						b.setDead(true);
						Explode ex = Explode.createExplode(bullet.getType()== BulletType.LASER?2:0, bullet.getCenterX(), bullet.getCenterY());
						mExplodes.add(ex);
						GameScreen.this.attachChild(ex);
						ex.start();
						break;
					}
				}
				
				if(!bullet.isDead()) {
					if(bullet.isCollisionWithMap(GameActivity.mGlobalMap) || bullet.isBreakOnMap(GameActivity.mGlobalMap)) 
					{
						bullet.setDead(true);
						Explode ex = Explode.createExplode(bullet.getType()==BulletType.LASER?2:1, bullet.getCenterX(), bullet.getCenterY());
						mExplodes.add(ex);
						GameScreen.this.attachChild(ex);
						ex.start();
					} else if (bullet.isCollisionWithITank(mItanks))
					{
						bullet.setDead(true);
						Explode ex = Explode.createExplode(bullet.getType()== BulletType.LASER?2:0, bullet.getCenterX(), bullet.getCenterY());
						mExplodes.add(ex);
						GameScreen.this.attachChild(ex);
						ex.start();
					}
					else
						bullet.onUpdate(GameActivity.mGlobalMap, second);
					
				}
				
				if(bullet.isDead()){
					mBullets.remove(i); i--;
					bullet.detachSelf();bullet.dispose();
				}
			}
			
			for(int i = 0; i< mExplodes.size(); i++){
				Explode ex = mExplodes.get(i);
				ex.onUpdate(null, second);
				if(ex.isDead()) {
					mExplodes.remove(i); i--;
					ex.detachSelf(); ex.dispose();
				}
			}
			
			for (int i = 0; i < mItanks.size(); i++) {
				ITank tank = mItanks.get(i);
				
				if(!tank.isDead()) {
					tank.findTarget(mTank);
					tank.onUpdate(GameActivity.mGlobalMap, second);
					if(tank.mIsAutoShoot){
						Bullet b = tank.shoot();
						mBullets.add(b); GameScreen.this.attachChild(b);
					}
				}
				else {
					mItanks.remove(i); i--;
					tank.detachSelf();
					if(tank==mTank)
						onTankDead(tank);
					else 
						onEnermyDead(tank);
				}
			}
			this.checkGameResult();
			
		}// end if(!mIsPause)
	}
	
	private void onTankDead(ITank tank){
		
		int currentHeart = tank.getHeart();
		if(currentHeart>0) {
			tank.setDead(false);
			tank.setX(0); tank.setY(0);
			tank.reset();
			mItanks.add(tank);
			attachChild(tank);
			tank.setHeart(currentHeart-1);
			mInfoBoard.setHeart(tank.getHeart());
		}else 
			this.onGameEnd(false);
	}
	
	private void showFlyText(String s,float x,float y){
		FlyText ft = new FlyText(x,y);
		ft.setText(s);
		attachChild(ft);
		mFlyTexts.add(ft);
	}
	/**
	 * Hàm xử lý khi 1 xe tăng địch bị bắn chết
	 * + Cộng điểm cho người chơi
	 * + Tạo xe tăng mới
	 * @param tank
	 */
	private void onEnermyDead(ITank tank){
		Enermy enermy = (Enermy)tank;
		switch(enermy.getType()){
		case NORMAL:
			if(mInfoBoard.getNormalTank() < GameActivity.mGlobalSetting.mNormalTank) {
				mInfoBoard.setNormalTank(mInfoBoard.getNormalTank()+1);
				mInfoBoard.setScore(mInfoBoard.getScore()+GameSetting.SCORE_ENERMY_NORMAL);
				showFlyText("+"+GameSetting.SCORE_ENERMY_NORMAL,enermy.getX(), enermy.getY());
			}
			break;
		case FAST:
			if(mInfoBoard.getFastTank() < GameActivity.mGlobalSetting.mFastTank) {
				mInfoBoard.setFastTank(mInfoBoard.getFastTank()+1);
				mInfoBoard.setScore(mInfoBoard.getScore()+GameSetting.SCORE_ENERMY_FAST);
				showFlyText("+"+GameSetting.SCORE_ENERMY_FAST,enermy.getX(), enermy.getY());
			}
			break;
		case HEAVY:
			if(mInfoBoard.getHeavyTank() < GameActivity.mGlobalSetting.mHeavyTank) {
				mInfoBoard.setHeavyTank(mInfoBoard.getHeavyTank()+1);
				mInfoBoard.setScore(mInfoBoard.getScore()+GameSetting.SCORE_ENERMY_HEAVY);
				showFlyText("+"+GameSetting.SCORE_ENERMY_HEAVY,enermy.getX(), enermy.getY());
			}
			break;
		case BOSS:
			enermy.setHeart(enermy.getHeart()-1);
			mInfoBoard.setScore(mInfoBoard.getScore()+GameSetting.SCORE_ENERMY_BOSS);
			showFlyText("+"+GameSetting.SCORE_ENERMY_BOSS,enermy.getX(), enermy.getY());
			if(enermy.getHeart() <= 0 ) {
				if( mInfoBoard.getBossTank() < GameActivity.mGlobalSetting.mBossTank) {
					mInfoBoard.setBossTank(mInfoBoard.getBossTank()+1);
				}
			}else {
				enermy.setDead(false);
				mItanks.add(enermy); attachChild(enermy);
			}
			break;
		}
		int i = 0;
		EnermyType type = null;
		do{
			type=null;
			i=mRadom.nextInt(4);
			switch(i){
			case 0:
				if(mCreateNormal < GameActivity.mGlobalSetting.mNormalTank) {
					type=EnermyType.NORMAL;
					mCreateNormal++;
				}
				break;
			case 1:
				if(mCreateFast < GameActivity.mGlobalSetting.mFastTank) {
					type=EnermyType.FAST;
					mCreateFast++;
				}
				break;
			case 2:
				if(mCreateHeavy < GameActivity.mGlobalSetting.mHeavyTank) {
					type=EnermyType.HEAVY;
					mCreateHeavy++;
				}
				break;
			}
		}while(type==null && !(mCreateNormal>= GameActivity.mGlobalSetting.mNormalTank&&
				mCreateFast>= GameActivity.mGlobalSetting.mFastTank&&
				mCreateHeavy>= GameActivity.mGlobalSetting.mHeavyTank) );
		
		if(type!=null){
			int[] newLoc = null;
			do{
				newLoc = GameActivity.mGlobalSetting.mOutLocation.get(mRadom.nextInt(GameActivity.mGlobalSetting.mOutLocation.size()));
				if(GameActivity.mGlobalMap.getMatrix()[newLoc[1]/32][newLoc[0]/32]!=0) {
					newLoc = null;
				}
			}while(newLoc==null);
			if(newLoc!=null){
				
				enermy.setType(type);
				enermy.setDead(false);
				enermy.setPosition(newLoc[0],newLoc[1]);
				enermy.reset();
				mItanks.add(enermy);
				attachChild(enermy);
			}
		}
	}
	
	/**
	 * +Kiểm tra xem kết thúc game hay chưa
	 * +Kiểm tra sinh xe tăng boss;
	 */
	private void checkGameResult(){
		
		if(mInfoBoard.getNormalTank() >= GameActivity.mGlobalSetting.mNormalTank&&
				mInfoBoard.getFastTank() >= GameActivity.mGlobalSetting.mFastTank&&
				mInfoBoard.getHeavyTank() >= GameActivity.mGlobalSetting.mHeavyTank) {
			// Nếu giết xe tăng boss
			if(mInfoBoard.getBossTank() >= GameActivity.mGlobalSetting.mBossTank)
				onGameEnd(true);
			// Ngược lại nếu còn xe tăng boss chưa tạo thì tạo hết
			
			for(int i=0;mCreateBoss < GameActivity.mGlobalSetting.mBossTank && i<GameActivity.mGlobalSetting.mBossTank;i++){
				
				int[] newLoc = null;
				Enermy boss = new Enermy(EnermyType.BOSS, 0,0);
				switch(GameActivity.mGlobalSetting.getCurrentLevel()){
				case 1:
					boss.setHeart(GameSetting.BOSS_LEVEL1_HEART);
					break;
				case 2:
					boss.setHeart(GameSetting.BOSS_LEVEL2_HEART);
					break;
				case 3:
					boss.setHeart(GameSetting.BOSS_LEVEL3_HEART);
					break;
				}
				
				do{
					newLoc = GameActivity.mGlobalSetting.mOutLocation.get(mRadom.nextInt(GameActivity.mGlobalSetting.mOutLocation.size()));
					if(GameActivity.mGlobalMap.getMatrix()[newLoc[1]/32][newLoc[0]/32]!=0) {
						newLoc = null;
					}
				}while(newLoc==null);
				
				if(newLoc!=null){
					boss.setDead(false);
					boss.setX(newLoc[0]);boss.setY(newLoc[1]);
					boss.reset();
					mItanks.add(boss);
					attachChild(boss);
					mCreateBoss++;
					
					BossHeartBar bhb = new BossHeartBar(boss, 
										GameActivity.CAMERA_WITDH/2-300/2,
										GameActivity.CAMERA_HEIGHT-(i+1)*(BossHeartBar.HEIGHT + 2*BossHeartBar.PADDING));
					mBossHeartBars.add(bhb); attachChild(bhb);
				}
				
			}// end for
		}
	}
	
	/**
	 * Hàm xử lý khi:
	 * +Người chơi hết mạng sống
	 * +Người chơi giết hết tất cả các xe tăng  boss( nếu có )
	 */
	private void onGameEnd(boolean isWin){
		mIsPause=true;
		if(isWin){
			int level = GameActivity.mGlobalSetting.getCurrentLevel()+1;
			if(level <= GameSetting.MAX_LEVEL) {
				if(level >= GameActivity.mGlobalSetting.getUnlockLevel()){
					GameActivity.mGlobalSetting.setUnlockLevel(level);
					GameActivity.saveSetting(GameActivity.DATABASE_FOLDER);
				}
			}
		}
		
		int nt = mSavedScore.mNormalTank + mInfoBoard.getNormalTank();
		int ft = mSavedScore.mFastTank + mInfoBoard.getFastTank();
		int ht = mSavedScore.mHeavyTank + mInfoBoard.getHeavyTank();
		int bt = mSavedScore.mBossTank +mInfoBoard.getBossTank();
		int sc = mSavedScore.mScore + mInfoBoard.getScore();
		mResultDialogScreen =new ResultScreen(isWin,GameActivity.mGlobalSetting.getCurrentLevel(),
													nt,ft,ht,bt,sc,this);
		setChildScene(mResultDialogScreen);
	}
	
	
	@Override
	public void onPlayAgain() {
		
		mCreateNormal = 0;
		mCreateFast = 0;
		mCreateHeavy = 0;
		mCreateBoss = 0;
		mInfoBoard.setHeart(GameSetting.PLAYER_HEART);
		mInfoBoard.setNormalTank(0);
		mInfoBoard.setFastTank(0);
		mInfoBoard.setHeavyTank(0);
		mInfoBoard.setBossTank(0);
		mInfoBoard.setScore(mSavedScore.mScore);
		
		if(getChildScene()== mResultDialogScreen && mResultDialogScreen!=null)
			mResultDialogScreen.detachSelf();
		if(GameActivity.loadLevelSetting(GameActivity.mGlobalSetting.getCurrentLevel())){
			
			mBullets.clear();
			mExplodes.clear();
			mItanks.clear();
			mFlyTexts.clear();
			mBossHeartBars.clear();
			
			
			mTank.setDir(MovingDirection.RIGHT);
			mTank.setSpeed(GameSetting.SPEED_TANK);
			mTank.setHeart(GameSetting.PLAYER_HEART); mTank.setDead(false);
			mTank.setX(0); mTank.setY(0);
			mTank.reset();
			for(int i = 0 ;i<GameActivity.mGlobalSetting.mOutLocation.size();i++){
				int[] location = GameActivity.mGlobalSetting.mOutLocation.get(i);
				EnermyType type = null;
				do{
					type=null;
					switch(mRadom.nextInt(3)){
					case 0:
						if(mCreateNormal < GameActivity.mGlobalSetting.mNormalTank) {
							type=EnermyType.NORMAL;
							mCreateNormal++;
						}
						break;
					case 1:
						if(mCreateFast < GameActivity.mGlobalSetting.mFastTank) {
							type=EnermyType.FAST;
							mCreateFast++;
						}
						break;
					case 2:
						if(mCreateHeavy < GameActivity.mGlobalSetting.mHeavyTank) {
							type=EnermyType.HEAVY;
							mCreateHeavy++;
						}
						break;
					}
				}while(type==null && !(mCreateNormal>= GameActivity.mGlobalSetting.mNormalTank&&
						mCreateFast>= GameActivity.mGlobalSetting.mFastTank&&
						mCreateHeavy>= GameActivity.mGlobalSetting.mHeavyTank) );
				
				if(type!=null){
					Enermy e = new Enermy(type,location[0],location[1]);
					mItanks.add(e);
				}
			}
			mItanks.add(mTank);
			
			detachChildren();
			this.clearChildScene();
			this.clearTouchAreas();
			
			GameActivity.mGlobalMap.showMap(this);
			for (ITank tank : mItanks){
				attachChild(tank);
			}
			
			this.setChildScene(mController);
			this.registerTouchArea(mShootController);
			this.attachChild(mShootController);
			
			attachChild(mInfoBoard);
			
			mSettingButton.setCurrentTileIndex(0);
			mSettingButton.setAlpha(0.5f); mSettingButton.setZIndex(1);
			this.registerTouchArea(mSettingButton); this.attachChild(mSettingButton);

			mPauseButton.setCurrentTileIndex(0);
			mPauseButton.setAlpha(0.5f); mPauseButton.setZIndex(1);
			this.registerTouchArea(mPauseButton); this.attachChild(mPauseButton);
			
			mHighScoreButton.setCurrentTileIndex(0);
			mHighScoreButton.setAlpha(0.5f);mHighScoreButton.setZIndex(1);
			this.registerTouchArea(mHighScoreButton); this.attachChild(mHighScoreButton);
		}
		mIsPause = false;
	}

	@Override
	public void onSaveHighScore(String name) {
		
		if(name!=null&&name.length()>0) {
			mSavedScore.mName = name;
			mSavedScore.mNormalTank += mInfoBoard.getNormalTank();
			mSavedScore.mFastTank += mInfoBoard.getFastTank();
			mSavedScore.mHeavyTank += mInfoBoard.getHeavyTank();
			mSavedScore.mBossTank += mInfoBoard.getBossTank();
			mSavedScore.mScore += mInfoBoard.getScore();
			mSavedScore.mTime = Utils.toTimeStamp(Calendar.getInstance().getTime());
			GameActivity.mDatabase.addHighScore(GameActivity.mGlobalSetting.getCurrentLevel(),mSavedScore);
		}
		mResultDialogScreen.detachSelf();
		ResourcesManager.getInstance().loadLevelSelectionResources();
		PickLevelScreen sc=new PickLevelScreen();
		SceneManager.getInstance().setScene(sc);
		
	}
	
	@Override
	public void onNextLevel() {
		mSavedScore.mNormalTank += mInfoBoard.getNormalTank();
		mSavedScore.mFastTank += mInfoBoard.getFastTank();
		mSavedScore.mHeavyTank += mInfoBoard.getHeavyTank();
		mSavedScore.mBossTank += mInfoBoard.getBossTank();
		mSavedScore.mScore += mInfoBoard.getScore();
		
		int level = GameActivity.mGlobalSetting.getCurrentLevel()+1;
		if(level <= GameSetting.MAX_LEVEL) {
			GameActivity.mGlobalSetting.setCurrentLevel(level);
			onPlayAgain();
		}
	}
	
}
